package logic

import (
	"context"
	"errors"
	"house/cache"
	"house/house"
	"house/internal/svc"
	"house/model/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateHouseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateHouseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateHouseLogic {
	return &UpdateHouseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateHouseLogic) UpdateHouse(in *house.UpdateHouseRequest) (*house.UpdateHouseResponse, error) {
	// todo: add your logic here and delete this line
	var s mysql.Shop
	result, _ := cache.Rdb.Get(l.ctx, "house").Result()
	if result == "" {
		logx.Error("商品发布已超过五分钟时效性")
		return nil, errors.New("商品发布已超过五分钟时效性")
	} else {
		s.Price = float64(in.Price)
		s.Num = in.Num
		s.Image = in.Image
		s.Info = in.Info
		s.Title = in.Title
		s.UserId = int(in.UserId)
		if err := s.Update(); err != nil {
			logx.Error("修改失败")
			return nil, errors.New("修改失败")
		}
	}
	return &house.UpdateHouseResponse{
		Id: int64(s.ID),
	}, nil
}
